<?php

/*
 * Author  : top-songshijie
 * Email   : 997031758@qq.com
 * DateTime: 2022/4/5 13:22
 */

namespace App\Http\Controllers;

use App\Request as AppRequest;
use Illuminate\Routing\Controller;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Validation\ValidatesRequests;
use App\Libs\SafeLibrary;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;

use App\Http\Middleware\RequestMiddleware;
use Illuminate\Http\Request;



class BaseController extends Controller
{

    
    public function __construct()
    {
        //$SafeLibrary=new SafeLibrary(2);
        if ((bool)env('REQUEST_LOG', false) == true) {
            $this->middleware('request');
        }
    }
    /**
     * 操作成功返回
     */
    protected function success($data=[], $msg = '操作成功', $code = 200)
    {
        if(empty($data)){
            $data=[];
        }
        $alert=[
            'code'    => $code,
            'message' => $msg,
            'data'    => $data
        ];
        echo json_encode($alert);
        exit;

    }
    protected function success1($data = [], $msg = '操作成功', $code = 200)
    {
        $alert='success';
        echo json_encode($alert);
        exit;
    }
    /**
     * 操作失败返回
     */
    protected function error($msg = '操作失败', $code = 0 , $data=[])
    {
        if(empty($data)){
            $data=[];
        }
        $alert=[
            'code'    => $code,
            'message' => $msg,
            'data'    => $data
        ];
        echo json_encode($alert);
        exit;

    }
    protected function error1($msg = '操作失败', $code = 0, $data = [])
    {
        $alert='failure';
        echo json_encode($alert);
        exit;
    }
    //校验规则
    protected function checkrule($data,$getRules, $getRulesMessage)
    {
        $validator = Validator::make($data,$getRules, $getRulesMessage);
        if($validator->fails()){
            $msg=[];
            $msg=[
                'code'=>'0',
                'msg'=>$validator->errors()->first(),
                'data'=> []
            ];
            echo json_encode($msg);
            exit;
        }
    }
    //根据经纬度算周边小区
    //根据用户提供的经纬度信息，计算查询结果与目标点的距离，并按距离升序排序，最后输出附近地点的名称、ID和距离。在测试部分，我们选择北京市中心的经纬度作为目标点，并设置了一个5公里的半径，以查找北京市中心周围5公里范围内的地点。xpoint：经度 ypoint：纬度
    //ypoint：纬度
    protected  function getNearbyLocations($latitude,$longitude, $radius = 10) {
        $earthRadius = 6371; // 地球半径，单位千米
        $sql = "SELECT id, name, xpoint, ypoint,
                ($earthRadius * 2 * ASIN(SQRT(
                    POWER(SIN(($latitude - ABS(ypoint)) * PI() / 180 / 2), 2) +
                    COS(latitude * PI() / 180) *
                    COS(ABS(ypoint) * PI() / 180) *
                    POWER(SIN(($longitude - xpoint) * PI() / 180 / 2), 2)
                ))) AS distance
                FROM locations
                HAVING distance<$radius
                ORDER BY distance";
        $list = DB::connection('mysql_erp')->select($sql);
        
    }

// 测试latitude = 39.9042;
/*longitude = 116.4074;radius = 5;
echo "Locations near (latitude: latitude, longitude:longitude) within radius km radius:<br>";
getNearbyLocations(latitude, longitude,radius);*/

}
